package com.bianguo.android.pigeondemo.retrofit;

import com.bianguo.android.pigeondemo.utils.MLog;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/6/4.
 */

public abstract class BaseSubscriber<T> implements Observer<T> {

    private HashMap<String, Disposable> disposables;
    private String disposableKey;

    public BaseSubscriber(HashMap<String, Disposable> disposables, String disposableKey) {
        this.disposables = disposables;
        this.disposableKey = disposableKey;
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        MLog.mLog("subscribe");
        disposables.put(disposableKey, d);
    }

    @Override
    public void onError(@NonNull Throwable e) {
        ApiException apiException = (ApiException) e;
        onError(apiException);
    }

    @Override
    public void onComplete() {
        if (disposables.size() <= 0) {
            return;
        }
        Disposable disposable = disposables.get(disposableKey);
        if (!disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    public abstract void onError(ApiException apiException);
}
